package com.neuedu.hrsystem.person.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.hrsystem.bean.Dept;
import com.neuedu.hrsystem.person.service.DeptService;

@Controller
@RequestMapping(value = "/dept")
public class DeptAction {
	@Autowired
	private DeptService deptService;

	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> pageMap = new HashMap<String, Object>();
		pageMap.put("startPage", (page - 1) * rows);
		pageMap.put("endPage", rows);
		List<Dept> d = deptService.findAll();
		int total = deptService.countDept();
		map.put("rows", d);
		map.put("total", total);
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	@RequestMapping(value = "addDept")
	public void addDept(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String dname = request.getParameter("dname");
		String leader = request.getParameter("leader");

		Dept d = new Dept();
		d.setDname(dname);
		d.setLeader(leader);

		int res = deptService.addDept(d);
		System.out.println(res);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "Save dept fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	@RequestMapping(value = "updateDept")
	public void updateDept(HttpServletRequest request, HttpServletResponse response) throws Exception {

		int deptno = Integer.parseInt(request.getParameter("deptno"));
		String dname = request.getParameter("dname");
		String leader = request.getParameter("leader");

		Dept d = new Dept();
		d.setDeptno(deptno);
		d.setDname(dname);
		d.setLeader(leader);

		int res = deptService.updateDept(d);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "update dept fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	@RequestMapping(value = "deleteDept")
	public void deleteDept(HttpServletRequest request, HttpServletResponse response) throws Exception {

		int deptno = Integer.parseInt(request.getParameter("deptno"));
		System.out.println(deptno);

		int res = deptService.deleteDept(deptno);

		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "update dept fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

}
